import Eos from "eosjs";
import React, { Component } from 'react';

class EOSApp extends React.Component {
    constructor(props) {
        super(props);
        this.state = { user1ActivePrivKey: '5JDFRAnuMbMWgZvzkdFXNoCFTdgChNZMEoyT7QsrHn8T1pNZ4e6', user2ActivePrivKey: '5K2iQEbXhC9nGgzsu6dsVsJKcTrFuwtXZjvfnp2zcJduDtPyVMz' };

        const config = {
            chainId: null, // 32 byte (64 char) hex string
            keyProvider: ['PrivateKeys...'], // WIF string or array of keys..
            httpEndpoint: 'http://127.0.0.1:8888',
            expireInSeconds: 60,
            broadcast: true,
            verbose: false, // API activity
            sign: true
        }

        const configDefaults = {
            chainId: null, // 32 byte (64 char) hex string
            httpEndpoint: 'http://192.168.160.132:8888',
            expireInSeconds: 60,
            debug: false,
            verbose: false,
            broadcast: true,
            keyProvider: [this.state.user1ActivePrivKey, this.state.user2ActivePrivKey],
            logger: {
                log: function log() {
                    var _console;

                    return config.verbose ? (_console = console).log.apply(_console, arguments) : null;
                },
                error: function error() {
                    var _console2;

                    return config.verbose ? (_console2 = console).error.apply(_console2, arguments) : null;
                }
            },
            sign: true
        };
        const eos = Eos(configDefaults);

        // This binding is necessary to make `this` work in the callback
        // this.render = this.render.bind(this);
        this.getBalance = this.getBalance.bind(this);
        this.transefer = this.transefer.bind(this);
        this.state.EOS = eos;
    }
    getBalance = (account) => {
        return new Promise((res, rej) => {
            this.state.EOS.getCurrencyBalance("eosio.token", account).then(tokenBalance => {
                // console.log(tokenBalance);
                res(tokenBalance)
            });
        });

    }
    //{ from: from, to: to, amount: amount, memo: memo }
    transefer = (obj) => {
        return new Promise((res, rej) => {
            this.state.EOS
                .transaction({
                    actions: [
                        {
                            account: "eosio.token",
                            name: "transfer",
                            authorization: [
                                {
                                    actor: obj.from,
                                    permission: "active"
                                }
                            ],
                            data: {
                                from: obj.from,
                                to: obj.to,
                                quantity: obj.amount + '.0000 MTT',
                                memo: obj.memo || "some description of the transaction"
                            }
                        }
                    ]
                })
                .then(result => {
                    // Check token balances again.  You'll now see 5 TOK in user2’s
                    // account and 95 TOK in user1’s account
                    // console.log(result);
                    res(result)
                });
        });
    }
}

export { EOSApp };